package com.restkeeper.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.restkeeper.entity.DyTimesRecord;
import com.restkeeper.service.DyTimesRecordService;
import com.restkeeper.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dyTimesRecord")
public class DyTimesRecordController {

    @Autowired
    private DyTimesRecordService dyTimesRecordService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") long current,
                       @RequestParam(defaultValue = "10") long size) {
        Page<DyTimesRecord> page = new Page<>(current, size);
        return Result.ok(dyTimesRecordService.page(page, new QueryWrapper<>()));
    }

    /**
     * 添加记录
     */
    @PostMapping
    public Result add(@RequestBody DyTimesRecord dyTimesRecord) {
        return dyTimesRecordService.save(dyTimesRecord) ? Result.ok() : Result.error();
    }

    /**
     * 修改记录
     */
    @PutMapping
    public Result update(@RequestBody DyTimesRecord dyTimesRecord) {
        return dyTimesRecordService.updateById(dyTimesRecord) ? Result.ok() : Result.error();
    }

    /**
     * 删除记录
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        return dyTimesRecordService.removeById(id) ? Result.ok() : Result.error();
    }
}